Skip to content

centralize -Zmin-function-alignment logic #142854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025

Conversation

folkertdev
Copy link
Contributor

tracking issue: #82232
discussed in: #142824 (comment)

Apply the -Zmin-function-alignment value to the alignment field of the function attributes when those are created, so that individual backends don't need to consider it.

The one exception right now is cranelift, because it can't yet set the alignment for individual functions, but it can (and does) set the global minimum function alignment.

cc @RalfJung I think this is an improvement regardless, is there anything else that should be done for miri?

@rustbot
Copy link
Collaborator

rustbot commented Jun 21, 2025

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 21, 2025

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_codegen_ssa/src/codegen_attrs.rs

cc @jdonszelmann

@jieyouxu
Copy link
Member

r? codegen

@workingjubilee
Copy link
Member

This looks good to me.

r? workingjubilee
@bors r+

@bors
Copy link
Collaborator

bors commented Jun 22, 2025

📌 Commit a123a36 has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 22, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 22, 2025
…-alignment, r=workingjubilee

centralize `-Zmin-function-alignment` logic

tracking issue: rust-lang#82232
discussed in: rust-lang#142824 (comment)

Apply the `-Zmin-function-alignment` value to the alignment field of the function attributes when those are created, so that individual backends don't need to consider it.

The one exception right now is cranelift, because it can't yet set the alignment for individual functions, but it can (and does) set the global minimum function alignment.

cc `@RalfJung` I think this is an improvement regardless, is there anything else that should be done for miri?
bors added a commit that referenced this pull request Jun 23, 2025
Rollup of 6 pull requests

Successful merges:

 - #140136 (Add an aarch64-msvc build running on ARM64 Windows)
 - #141597 (Document subdirectories of UI tests with README files)
 - #142823 (Port `#[no_mangle]` to new attribute parsing infrastructure)
 - #142828 (1.88.0 release notes)
 - #142854 (centralize `-Zmin-function-alignment` logic)
 - #142875 (Check rustdoc-json-types FORMAT_VERSION is correctly updated)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Jun 23, 2025
Rollup of 5 pull requests

Successful merges:

 - #141597 (Document subdirectories of UI tests with README files)
 - #142823 (Port `#[no_mangle]` to new attribute parsing infrastructure)
 - #142828 (1.88.0 release notes)
 - #142854 (centralize `-Zmin-function-alignment` logic)
 - #142875 (Check rustdoc-json-types FORMAT_VERSION is correctly updated)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 57abad8 into rust-lang:master Jun 23, 2025
10 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 23, 2025
rust-timer added a commit that referenced this pull request Jun 23, 2025
Rollup merge of #142854 - folkertdev:centralize-min-function-alignment, r=workingjubilee

centralize `-Zmin-function-alignment` logic

tracking issue: #82232
discussed in: #142824 (comment)

Apply the `-Zmin-function-alignment` value to the alignment field of the function attributes when those are created, so that individual backends don't need to consider it.

The one exception right now is cranelift, because it can't yet set the alignment for individual functions, but it can (and does) set the global minimum function alignment.

cc ``@RalfJung`` I think this is an improvement regardless, is there anything else that should be done for miri?
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 23, 2025
…no-attributes, r=workingjubilee

fix `-Zmin-function-alignment` on functions without attributes

tracking issue: rust-lang#82232
related: rust-lang#142854

The minimum function alignment was skipped on functions without attributes (because the logic was in a loop that only runs if there is at least one attribute). The underlying reason we didn't catch this before is that in our testing we generally apply `#[no_mangle]` to functions that are tested. I've added a test now that deliberately has no attributes.

r? `@workingjubilee`
rust-timer added a commit that referenced this pull request Jun 24, 2025
Rollup merge of #142923 - folkertdev:min-function-alignment-no-attributes, r=workingjubilee

fix `-Zmin-function-alignment` on functions without attributes

tracking issue: #82232
related: #142854

The minimum function alignment was skipped on functions without attributes (because the logic was in a loop that only runs if there is at least one attribute). The underlying reason we didn't catch this before is that in our testing we generally apply `#[no_mangle]` to functions that are tested. I've added a test now that deliberately has no attributes.

r? `@workingjubilee`
@@ -126,6 +126,12 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
}
}

// Apply the minimum function alignment here, so that individual backends don't have to.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually more important than that, if you consider that -C flags can differ between translation units.

This ensures functions defined in a crate compiled with -Cmin-function-alignment get that alignment independent of the crate they are codegen'd in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a function from crate A is generated in another crate B compiled with -Zmin-function-alignment=B, I would expect the alignment to be at least B.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't say anything about which function is generated where, so I don't see how such a guarantee could be practically useful?

From a const-eval perspective, doing this would be just unsound as it means the result of "which alignment does this function have" could be different in different parts of the program.

// Function alignment can be set globally with the `-Zmin-function-alignment=<n>` flag;
// the alignment from a `#[repr(align(<n>))]` is used if it specifies a higher alignment.
let fn_align = self.tcx.codegen_fn_attrs(instance.def_id()).alignment;
let global_align = self.tcx.sess.opts.unstable_opts.min_function_alignment;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way we can have a lint against accessing tcx.sess.opts in the interpreter? Or is there some way the query machinery could help us ensure this? This is a soundness bug waiting to happen. :/ And it can't be just the interpreter, right? Other queries also must be cross-crate-stable in the same way?

samueltardieu added a commit to samueltardieu/rust that referenced this pull request Jun 26, 2025
codegen_fn_attrs: make comment more precise

Follow-up to rust-lang#142854.

r? `@oli-obk` or `@workingjubilee`
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jun 26, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#141597 (Document subdirectories of UI tests with README files)
 - rust-lang/rust#142823 (Port `#[no_mangle]` to new attribute parsing infrastructure)
 - rust-lang/rust#142828 (1.88.0 release notes)
 - rust-lang/rust#142854 (centralize `-Zmin-function-alignment` logic)
 - rust-lang/rust#142875 (Check rustdoc-json-types FORMAT_VERSION is correctly updated)

r? `@ghost`
`@rustbot` modify labels: rollup
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 26, 2025
codegen_fn_attrs: make comment more precise

Follow-up to rust-lang#142854.

r? ``@oli-obk`` or ``@workingjubilee``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants